//
//  UIView+Extension.swift
//  NewTradmarkSerice
//
//  Created by hlt on 2023/4/6.
//

import UIKit
enum GradientLayerAxiType{
   case GradientLayerHAxi//水平方向轴渐变
   case GradientLayerVAxi//竖直方向轴渐变
   case GradientLayerDAxi//对角方向轴渐变
}

extension UIView{
    @discardableResult
    func setGradualLeftColor(_ fromColor:UIColor,_ toColor:UIColor,_ axiType:GradientLayerAxiType = .GradientLayerHAxi) -> CALayer{
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds;
        
        gradientLayer.frame = self.bounds;
        //  设置颜色变化点，取值范围 0.0~1.0
        gradientLayer.locations = [0, 1];
        
        //  创建渐变色数组，需要转换为CGColor颜色
        gradientLayer.colors = [fromColor.cgColor,toColor.cgColor];
      
        if(axiType == .GradientLayerHAxi){
            //0.0左上-1.0右上,水平方向的渐变
            //或者0,1左下 - 1.1右下,水平方向的渐变
            gradientLayer.startPoint = CGPointMake(0, 0);//CGPointMake(0, 1)
            gradientLayer.endPoint = CGPointMake(1, 0);//CGPointMake(1, 1)
        }else if (axiType == .GradientLayerDAxi){
            //对角方向的渐变
            gradientLayer.startPoint = CGPointMake(0, 0);
            gradientLayer.endPoint = CGPointMake(1, 1);
        }
        else{
            //0,0左上-0,1左上,竖直方向的渐变
            //或者1,0右上 - 1.1右下,竖直方向的渐变
            gradientLayer.startPoint = CGPointMake(0, 0);//CGPointMake(1, 0)
            gradientLayer.endPoint = CGPointMake(0, 1);//CGPointMake(1, 1)
        }
        self.layer.insertSublayer(gradientLayer, at: 0)
        
        return gradientLayer
    }
    
    
    
    
    
    
 
    //绘制阴影和圆角
    func setShadowLayerWithRadius(shadowOffset:CGSize,shadowColor:UIColor,shadowOpaci:Float,shadowRadius:Float,cornerRadius:Float,borderColor:UIColor? = nil,borderWidth:Float? = nil){
        let shadowLayer = CALayer()
        self.layer.insertSublayer(shadowLayer, at: 0)
        //[view.layer insertSublayer:shadowLayer atIndex:0];
        shadowLayer.frame = self.bounds;
        shadowLayer.shadowOffset =  shadowOffset

        shadowLayer.shadowColor  =  shadowColor.cgColor
        shadowLayer.shadowOpacity = shadowOpaci;
        shadowLayer.shadowRadius = CGFloat(shadowRadius);
        let contentLayer = CALayer();
        contentLayer.frame = shadowLayer.bounds;
        contentLayer.backgroundColor =  self.backgroundColor?.cgColor;
        self.backgroundColor =  UIColor.clear
        contentLayer.cornerRadius = 8
        contentLayer.masksToBounds = true
        contentLayer.borderColor = UIColor.blue.cgColor;
        contentLayer.borderWidth = 0;
        shadowLayer.addSublayer(contentLayer)
    }
}


struct CornerRadii{
    var topLeft = 0.0
    var topRight = 0.0
    var bottomLeft = 0.0
    var bottomRight = 0.0
}
extension UIView{
    func setRoundedCorners(_ cornerRadii: CornerRadii) {
        self.setLayerRoundeCorners(self.layer, radii: cornerRadii)
        
    }
    func setLayerRoundeCorners(_ layer: CALayer, radii cornerRadii: CornerRadii) {
        let bounds = self.bounds
        let minX = bounds.minX
        let minY = bounds.minY
        let maxX = bounds.maxX
        let maxY = bounds.maxY
          
        let topLeftCenterX = minX + cornerRadii.topLeft
        let topLeftCenterY = minY + cornerRadii.topLeft
          
        let topRightCenterX = maxX - cornerRadii.topRight
        let topRightCenterY = minY + cornerRadii.topRight
          
        let bottomLeftCenterX = minX + cornerRadii.bottomLeft
        let bottomLeftCenterY = maxY - cornerRadii.bottomLeft
          
        let bottomRightCenterX = maxX - cornerRadii.bottomRight
        let bottomRightCenterY = maxY - cornerRadii.bottomRight
      
        let path = UIBezierPath()
        // 顶 左
        path.addArc(withCenter: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: cornerRadii.topLeft, startAngle: CGFloat.pi, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
            
        // 顶 右
        path.addArc(withCenter: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: cornerRadii.topRight, startAngle: 3 * CGFloat.pi / 2, endAngle: 0, clockwise: true)
            
        // 底 右
        path.addArc(withCenter: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: cornerRadii.bottomRight, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: true)
            
        // 底 左
        path.addArc(withCenter: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: cornerRadii.bottomLeft, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: true)
        path.close()
          
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        layer.mask = shapeLayer
    }
}
